package com.itheima.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Test003 {

    //实现使用FileChannel结合MappedByteBuffer实现2G以下文件复制的例子。 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile f1 = new RandomAccessFile("D\\123.zip","r");
        RandomAccessFile f2 = new RandomAccessFile("E\\复制.zip","rw");
        //获取通道;
        FileChannel c1 = f1.getChannel();
        FileChannel c2 = f2.getChannel();


        //获取文件大小;
        long size = c1.size();
        //映射;
        MappedByteBuffer buffer1 = c1.map(FileChannel.MapMode.READ_ONLY, 0, size);
        MappedByteBuffer buffer2 = c2.map(FileChannel.MapMode.READ_WRITE, 0, size);

        //读写
        for (long i = 0; i < size; i++) {
            buffer2.put(buffer1.get());
        }
        //关流;
        f1.close();
        f2.close();
        c1.close();
        c2.close();




    }


}
